package day1

import org.junit.Test
import kotlin.test.assertEquals

class Sum {
    fun sum(nums: IntArray, target: Int): IntArray {
        //将数组转换为map,索引为key，值为value
        nums.withIndex().forEach { (index, value) ->
            val remaining=target-value
            if (nums.contains(remaining) && nums.indexOf(remaining)!=index){
                return intArrayOf(index,nums.indexOf(remaining)).sortedArray()
            }
        }
        return intArrayOf()
    }

    @Test
    fun test1() {
        val nums = intArrayOf(2, 7, 11, 15)
        val target = 9
        val result = Sum().sum(nums, target)
        assertEquals("[0, 1]",result.contentToString())
    }

    @Test
    fun test2(){
        val nums = intArrayOf(3, 2, 4)
        val target = 6
        val result = Sum().sum(nums, target)
        assertEquals("[1, 2]",result.contentToString())
    }

    @Test
    fun test3(){
        val nums = intArrayOf(3, 3)
        val target = 6
        val result = Sum().sum(nums, target)
        assertEquals("[0, 1]",result.contentToString())
    }
}

